<?php
/**
 * common model file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/09/04
 * Time: 17:52
 */

namespace HiCommon\Model;

use Modules\Api\Service\CheckpointOrderService;

class OrderCheckpoint extends Base
{

    protected $table = 'order_checkpoint';

    protected $guarded = [];

    //public $timestamps = false;

    /**
     * 应该被调整为日期的属性
     *
     * @var array
     */
    protected $dates = [
    ];

    /**
     * 关联检车表
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function apply_vehicle(){
        return $this->hasMany(OrderCheckpointVehicle::class,'order_checkpoint_id')->select(['id','order_checkpoint_id','check_state','apply_refund_state','price']);
    }


    /**
     * 关联商家表
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function shop(){
        return $this->belongsTo(Shop::class,'shop_id')->select(['id','name','logo','phone','address','longitude','latitude']);
    }

    /**
     * 关联用户表
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function user(){
        return $this->belongsTo(Users::class,'user_id')->select(['id','nickname','phone','realname','head_img']);
    }

    /**
     * 关联用户表
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function user_company(){
        return $this->belongsTo(UsersCompany::class,'user_company_id')->select(['id','company_logo','company_name']);
    }

    /**
     * 关联检车表
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function check_vehicle(){
        return $this->hasMany(OrderCheckpointVehicle::class,'order_checkpoint_id');
    }

    /**
     * 关联预约单评价表
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function shop_checkpoint_apprise(){
        return $this->hasOne(UsersApprise::class,'order_id')->where('type',11)->select(['id','type','shop_id','order_id','goods_id','score','content','user_id']);
    }

    /**
     * 关联微信退款表
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function wechat_refund(){
        return $this->hasOne(OrderPayRefund::class,'order_sn','order_sn')->select(['id', 'order_sn', 'out_trade_no', 'out_refund_no', 'refund_fee', 'total_fee', 'type']);
    }

    /**
     * 关联支付表
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function order_pay(){
        return $this->hasOne(OrderPay::class,'order_sn','order_sn');
    }

    /**
     * 关联退款表
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function order_pay_refund(){
        return $this->hasMany(OrderPayRefund::class,'order_sn','out_trade_no');
    }

    /**
     * 获取状态对应的名称
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getStateAttribute($value)
    {
        switch ($value)
        {
            case 0:
                $res = ['id'=>0,'name'=>'草稿'];
                break;
            case 1:
                $res = ['id'=>1,'name'=>'待审核'];
                break;
            case 2:
                $res = ['id'=>2,'name'=>'通过'];
                break;
            case 3:
                $res = ['id'=>3,'name'=>'拒绝'];
                break;
            default:
                $res = ['id'=>-1,'name'=>'未知'];
        }
        return $res;
    }

    /**
     * 获取图片可用链接
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getImgUrlAttribute($value)
    {
        $arr_data = [
          'src'=>$value,
          'full_src'=>oss_full_url($value),
        ];
        return $arr_data;
    }

    //支付状态：1待支付 2已支付
    public function name_pay_state($state,&$arr_data){
        switch ($state)
        {
            //支付状态：1待支付 2已支付
            case 2:
                $arr_data['pay_state_name'] = '已支付';
                break;
            case 1:
                $arr_data['pay_state_name'] = '未支付';
                break;
            default:
                $arr_data['pay_state_name'] = '';
        }
    }

    //退款状态：0默认 1未退款 2部分退款 3全部退款-整单退款 4全部退款-逐个累计
    public function name_refund_state($state,&$arr_data){
        switch ($state)
        {
            case 1:
                $arr_data['refund_state_name'] = '未退款';
                break;
            case 2:
                $arr_data['refund_state_name'] = '部分退款';
                break;
            case 3:
                $arr_data['refund_state_name'] = '全部退款';
                break;
            case 4:
                $arr_data['refund_state_name'] = '全部退款';
                break;
            default:
                $arr_data['refund_state_name'] = '';
        }

    }

    //预约状态：1预约中 2预约成功 3预约失败 [9预约取消]
    public function name_appointment_state($state,&$arr_data){
        switch ($state)
        {
            case 1:
                $arr_data['appointment_state_name'] = '预约中';
                break;
            case 2:
                $arr_data['appointment_state_name'] = '预约成功';
                break;
            case 3:
                $arr_data['appointment_state_name'] = '预约失败';
                break;
            default:
                $arr_data['appointment_state_name'] = '';
        }

    }

    //订单完成状态：0默认 1待完成-等待用户确认完成 2已完成-用户已确认完成 3已结束-订单全部退款已终止 -1待检测
    public function name_complete_state($state,&$arr_data){
        switch ($state)
        {
            case -1:
                $arr_data['complete_state_name'] = '待检测';
                break;
            case 1:
                $arr_data['complete_state_name'] = '待完成';
                break;
            case 2:
                $arr_data['complete_state_name'] = '已完成';
                break;
            case 3:
                $arr_data['complete_state_name'] = '已结束';
                break;
            default:
                $arr_data['complete_state_name'] = '';
        }

    }

    //评价状态：0默认 1待评价 2已评价
    public function name_appraise_state($state,&$arr_data){
        switch ($state)
        {
            case 1:
                $arr_data['appraise_state_name'] = '待评价';
                break;
            case 2:
                $arr_data['appraise_state_name'] = '已评价';
                break;
            default:
                $arr_data['appraise_state_name'] = '';
        }

    }

    /**
     * 扩展信息
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getExtendAttribute()
    {
        //订单类型 个人单或企业单
        $n_main_type = $this->main_type;
        $s_main_type_name = '';
        if(!is_null($n_main_type)){
            $n_main_type = intval($n_main_type);
            if($n_main_type == 1){
                $s_main_type_name = '个人单';
            }
            else{
                $s_main_type_name = '企业单';
            }
        }
        $arr_data['main_type'] = $s_main_type_name;

        //支付类型
        $s_payment_method = $this->payment_method;
        $s_payment_method_name = '';
        if(!is_null($s_payment_method)){
            $s_payment_method = str_trim($s_payment_method);
            switch ($s_payment_method)
            {
                //wxmini-微信小程序支付 wxapp-微信app支付 aliapp-支付宝app支付
                case 'wxmini':
                    $s_payment_method_name = '微信小程序支付';
                    break;
                case 'wxapp':
                    $s_payment_method_name = '微信app支付';
                    break;
                case 'aliapp':
                    $s_payment_method_name = '支付宝app支付';
                    break;
                default:
                    $s_payment_method_name = '';
            }

        }
        $arr_data['payment_method'] = $s_payment_method_name;

        //订单状态
        $arr_data['pay_state'] = $this->pay_state;
        $arr_data['refund_state'] = $this->refund_state;
        $arr_data['appointment_state'] = $this->appointment_state;
        $arr_data['complete_state'] = $this->complete_state;
        $arr_data['appraise_state'] = $this->appraise_state;
        OrderCheckpoint::deal_for_state($arr_data);
        return $arr_data;
    }



    //合并订单状态
    public function combine_state($n_pay_state = 0,$n_refund_state = 0,$n_appointment_state = 0,$n_complete_state = 0,$n_appraise_state = 0,&$arr_data){

        // 10-待支付 20预约取消（已支付-已取消） 21-预约取消（未支付-已取消） 22-预约成功 30-待确认完成 31-已完成-待评价 32-已完成-已评价 40 -1状态异常
        $n_goto_pay = 2;//立即支付 1是 2否
        $n_cancel_order = 2;//取消预约 1是 2否
        $n_goto_apprise = 2;//立即评价 1是 2否
        $n_del_order = 2;//删除订单 1是 2否
        $n_waiting_confirm = 2;//确认完成 1是 2否
        $n_order_state = -1;
        $s_order_state_name = '状态异常';

//        if($n_complete_state == 1){
//            $n_waiting_confirm = 1;
//        }

        //下单
        if($n_pay_state == 1 && $n_appointment_state == 1){
            $n_order_state = 10;
            $s_order_state_name = '待支付';
            $n_goto_pay = 1;
            $n_cancel_order = 1;
        }

        //取消预约
        if($n_pay_state == 1 && $n_appointment_state == 3){
            $n_order_state = 21;
            $s_order_state_name = '已取消';//未支付
            $n_del_order = 1;

        }

        //已取消
        if($n_pay_state == 2 && $n_appointment_state == 3){
            $n_order_state = 21;//后期修改成23
            $s_order_state_name = '已取消';//已支付
            $n_del_order = 1;

        }

        //取消预约
        if($n_pay_state == 2 && $n_appointment_state == 3 && $n_complete_state == 3){
            $n_order_state = 21;//后期修改成23
            $s_order_state_name = '已终止';//已支付
            $n_del_order = 1;

        }

        //支付
        if ($n_complete_state == -1){
//            if ($n_pay_state == 2 && $n_appointment_state == 2 && $n_complete_state == -1){
            $n_order_state = 22;
            $s_order_state_name = '预约成功';
            //检测和申请退款要根据子单状态判断
        }

        //商户检测完成
        if ($n_complete_state == 1){
//            if ($n_pay_state == 2 && $n_appointment_state == 2 && $n_complete_state == 1){
            $n_order_state = 30;
            $s_order_state_name = '待确认完成';
            $n_waiting_confirm = 1;
        }

        //用户确认完成
        if ($n_complete_state == 2 && $n_appraise_state == 1){
//            if ($n_pay_state == 2 && $n_appointment_state == 2 && $n_complete_state == 2 && $n_appraise_state == 1){
            $n_goto_apprise = 1;
            $n_order_state = 31;
            $s_order_state_name = '待评价';

        }

        if ($n_complete_state == 2 && $n_appraise_state == 2){
            //            if ($n_pay_state == 2 && $n_appointment_state == 2 && $n_complete_state == 2 && $n_appraise_state == 1){
            $n_order_state = 32;
            $s_order_state_name = '已评价';

        }


        //用户确认完成
        if ($n_appraise_state == 2 || $n_complete_state == 3){
            $n_del_order = 1;
        }


        $arr_data['order_state'] = $n_order_state;
        $arr_data['order_state_name'] = $s_order_state_name;
        $arr_data['user_waiting_operate'] = [
          'goto_pay'=>$n_goto_pay,
          'cancel_order'=>$n_cancel_order,
          'goto_apprise'=>$n_goto_apprise,
          'del_order'=>$n_del_order,
          'waiting_confirm'=>$n_waiting_confirm
        ];
    }

    public function deal_for_state(&$arr_data){
        $n_pay_state = intval($arr_data['pay_state']);
        $n_refund_state = intval($arr_data['refund_state']);
        $n_appointment_state = intval($arr_data['appointment_state']);
        $n_complete_state = intval($arr_data['complete_state']);
        $n_appraise_state = intval($arr_data['appraise_state']);

        //支付状态：1待支付 2已支付
        self::name_pay_state($n_pay_state,$arr_data);

        //退款状态：0默认 1未退款 2部分退款 3全部退款-整单退款 4全部退款-逐个累计
        self::name_refund_state($n_refund_state,$arr_data);

        //预约状态：1预约中 2预约成功 3预约失败 [9预约取消]
        self::name_appointment_state($n_appointment_state,$arr_data);

        //订单完成状态：0默认 1待完成-等待用户确认完成 2已完成-用户已确认完成 3已结束-订单全部退款已终止 -1待检测
        self::name_complete_state($n_complete_state,$arr_data);

        //评价状态：0默认 1待评价 2已评价
        self::name_appraise_state($n_appraise_state,$arr_data);

        self::combine_state($n_pay_state,$n_refund_state,$n_appointment_state,$n_complete_state,$n_appraise_state,$arr_data);
    }
//
//    /**
//     * 处理年检预约单状态
//     *  状态说明：-1 错误状态 0 状态不全 1 待支付 20 待检测 21 待评价 22 已评价 30 已取消-[未预约成功后 取消] 31 已取消-[预约成功后 取消] 40 检测失败
//     *
//     * @param int $n_pay_state            支付状态：1未支付 2已支付
//     * @param int $n_refund_state         退款状态：0默认 1未退款 2部分退款 3全部退款-整单退款 4全部退款-逐个累计
//     * @param int $n_appointment_state    预约状态：1预约中 2预约成功 3预约失败 9预约取消
//     * @param int $n_check_state          检测状态：0默认 1待检测 2检测完成 9检测失败
//     * @param int $n_appraise_state       评价状态：0默认 1待评价 2已评价
//     *
//     * @return int                      -1  错误状态
//     *                                  0   状态不全
//     *
//     *                                  1   待支付
//     *
//     *                                  20  待检测
//     *                                  21  待评价
//     *                                  22  已评价
//     *
//     *                                  30  已取消-[未预约成功后 取消]
//     *                                  31  已取消-[预约成功后 取消]
//     *
//     *                                  40  检测失败
//     * @author wumengmeng <wu_mengmeng@foxmail.com>
//     */
//    protected function deal_order_checkpoint_state($n_pay_state = 0,	$n_refund_state = 0, $n_appointment_state = 0, $n_check_state = 0, $n_appraise_state = 0){
//
//        //状态不全-错误状态
////        $state_0 = ($n_pay_state == 0) || ($n_refund_state == 0) || ($n_appointment_state == 0) || ($n_check_state == 0) || ($n_appraise_state == 0);
//
//        //待支付-[未预约成功]
//        $state_1 = ($n_pay_state == 1) && ($n_refund_state == 0) && ($n_appointment_state == 1) && ($n_check_state == 0) && ($n_appraise_state == 0);
//
//        //预约中 【功能取消】
////        $state_10 = ($n_pay_state == 2) && ($n_refund_state == 1) && ($n_appointment_state == 1) && ($n_check_state == 1) && ($n_appraise_state == 1);
//
//        //待检测-[预约成功 已支付] $n_refund_state=2 可能定义为检测中
//        $state_20 = ($n_pay_state == 2) && ($n_refund_state == 0 || $n_refund_state == 2) && ($n_appointment_state == 2) && ($n_check_state == 1) && ($n_appraise_state == 0);
//
//        //待评价-[预约成功 检测完成]
//        $state_21 = ($n_pay_state == 2) && ($n_refund_state == 0 || $n_refund_state == 2) && ($n_appointment_state == 2) && ($n_check_state == 2) && ($n_appraise_state == 1);
//
//        //已评价-[预约成功 检测完成]
//        $state_22 = ($n_pay_state == 2) && ($n_refund_state == 0 || $n_refund_state == 2) && ($n_appointment_state == 2) && ($n_check_state == 2) && ($n_appraise_state == 2);
//
//        /*已取消订单*/
//        //已取消-[未预约成功后 取消]
//        $state_30 = ($n_pay_state == 1) && ($n_refund_state == 0) && ($n_appointment_state == 9) && ($n_check_state == 0) && ($n_appraise_state == 0);
//
//        //已取消-[预约成功后 取消]
//        $state_31 = ($n_pay_state == 2) && ($n_refund_state == 3) && ($n_appointment_state == 9) && ($n_check_state == 0) && ($n_appraise_state == 0);
//
//        /*检测失败订单*/
//        //检测失败-[ $n_refund_state == 3 一次性退全款] [$n_refund_state == 4 逐个累计退全款]
//        $state_40 = ($n_pay_state == 2) && ($n_refund_state == 3 || $n_refund_state == 4) && ($n_appointment_state == 2) && ($n_check_state == 9) && ($n_appraise_state == 0);
//
//        //预约失败【功能取消】
////        $state_90 = ($n_pay_state == 2) && ($n_refund_state == 3) && ($n_appointment_state == 3) && ($n_check_state == 1) && ($n_appraise_state == 1);
//
////        if($state_0){
////            return 0;
////        }
//        if($state_1){
//            return 1;
//        }
////        elseif($state_10){
////            return 10;
////        }
//        elseif($state_20){
//            return 20;
//        }
//        elseif($state_21){
//            return 21;
//        }
//        elseif($state_22){
//            return 22;
//        }
//        elseif($state_30){
//            return 30;
//        }
//        elseif($state_31){
//            return 31;
//        }
//        elseif($state_40){
//            return 40;
//        }
////        elseif($state_90){
////            return 90;
////        }
//        else{
//            return -1;
//        }
//    }
//
//    protected function order_state_arr(){
//        $arr_order_state = [
//          -1 => ['id'=>-1,'name'=>'错误状态'] ,
//          0 => ['id'=>0,'name'=>'状态不全'] ,
//          1 => ['id'=>1,'name'=>'待支付'] ,
//          20 => ['id'=>20,'name'=>'待检测'] ,
//          21 => ['id'=>21,'name'=>'待评价'] ,
//          22 => ['id'=>22,'name'=>'已评价'] ,
//          30 => ['id'=>30,'name'=>'已取消'] ,
//          31 => ['id'=>31,'name'=>'已取消'] ,
//          40 => ['id'=>40,'name'=>'检测失败'] ,
//        ];
//        return $arr_order_state;
//    }
//
//    protected function state_replace($arr){
//        if(isset($arr)){
//            return $arr;
//        }
//        else{
//            return ['id'=>0,'name'=>'无'];
//        }
//
//    }
//
//    protected function state_name_arr(){
//        //主体类型 1个人 2公司
//        $arr_main_type = [
//          1 => ['id'=>1,'name'=>'个人单'] ,
//          2 => ['id'=>2,'name'=>'企业单'] ,
//        ];
//
//        //支付方式：alipay支付宝 wechat_pay微信支付
//        $arr_payment_method = [
//          'alipay' => ['id'=>'alipay','name'=>'支付宝'] ,
//          'wechat_mini_pay' => ['id'=>'wechat_mini_pay','name'=>'微信'] ,
//        ];
//
//        //支付状态：1未支付 2已支付
//        $arr_pay_state = [
//          1 => ['id'=>1,'name'=>'未支付'] ,
//          2 => ['id'=>2,'name'=>'已支付'] ,
//        ];
//
//        //退款状态：1未退款 2部分退款 3全部退款-整单退款 4全部退款-逐个累计
//        $arr_refund_state = [
//          1 => ['id'=>1,'name'=>'未退款'] ,
//          2 => ['id'=>2,'name'=>'部分退款'] ,
//          3 => ['id'=>3,'name'=>'全部退款'] ,
//          4 => ['id'=>4,'name'=>'全部退款'] ,
//        ];
//
//        //预约状态：1 预约中 2 预约成功 3 预约失败 9 预约取消
//        $arr_appointment_state = [
//          1 => ['id'=>1,'name'=>'预约中'] ,
//          2 => ['id'=>2,'name'=>'预约成功'] ,
//          3 => ['id'=>3,'name'=>'预约失败'] ,
//          9 => ['id'=>9,'name'=>'预约取消'] ,
//        ];
//
//        //检测状态：1 未开始 2 检测完成
//        $arr_check_state = [
//          1 => ['id'=>1,'name'=>'未开始'] ,
//          2 => ['id'=>2,'name'=>'检测完成'] ,
//        ];
//
//        //评价状态：1未评价 2已评价
//        $arr_appraise_state = [
//          1 => ['id'=>1,'name'=>'未评价'] ,
//          2 => ['id'=>2,'name'=>'已评价'] ,
//        ];
//
//        return [
//          $arr_main_type,
//          $arr_payment_method,
//          $arr_pay_state,
//          $arr_refund_state,
//          $arr_appointment_state,
//          $arr_check_state,
//          $arr_appraise_state
//        ];
//    }






}